/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uca.sisbec.helpers.manto;

import com.uca.sisbec.beans.CodeAndName;
import com.uca.sisbec.beans.CodeAndNameMateria;
import com.uca.sisbec.forms.manto.MateriaForm;
import com.uca.sisbec.mapping.*;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;

/**
 *
 * @author Susy
 */
public class MateriaHelper {

    private Session session;

    public MateriaHelper(Session session) {
        this.session = session;
    }
    
    public List<Universidad> getAllUniversidades(){
        List<Universidad> allUniversidades;
        Query queryResult = session.createQuery("From Universidad where estado='A'");
        allUniversidades = new ArrayList<Universidad>(queryResult.list());
        return allUniversidades;
    }
    
    public List<CodeAndName> getAvailableCarreras(int idUniv){
        String hql = "select new com.uca.sisbec.beans.CodeAndName(c.id.codCarrera, c.carrera) "
                + "from Carrera c where c.id.idUniv = :v_univ";
        Query query = session.createQuery(hql);
        System.out.println(idUniv);
        query.setInteger("v_univ", idUniv);
        ArrayList<CodeAndName> codAndName = (ArrayList<CodeAndName>) query.list();
        System.out.println(codAndName.size());
        return codAndName;
    }
    
    public List<CodeAndName> getAvailableMaterias(int codigo) {
        String hql = "select new com.uca.sisbec.beans.CodeAndNameMateria(m.codMateria, m.nombre) "
                + "from Materia m "
                + "where m.codMateria not in "
                + "(select mxc.materia.codMateria from MateriaXCarrera mxc "
                + "where mxc.id.codCarrera = :v_carrera order by m.nombre) "
                + "order by m.nombre";
        Query query = session.createQuery(hql);
        query.setInteger("v_carrera", codigo);
        return new ArrayList<CodeAndName>(query.list());
    }
    
    public List<CodeAndName> getSelectedMaterias(int codigo) {
        String hql = "select new com.uca.sisbec.beans.CodeAndNameMateria(m.codMateria, m.nombre) "
                + "from Materia m "
                + "where m.codMateria in "
                + "(select mxc.materia.codMateria from MateriaXCarrera mxc "
                + "where mxc.id.codCarrera = :v_carrera) "
                + "order by m.nombre";
        Query query = session.createQuery(hql);
        query.setInteger("v_carrera", codigo);
        return new ArrayList<CodeAndName>(query.list());
    }
    
    public void guardarMateriaDB(MateriaForm mf){
        Materia m = new Materia();
        m.setCodMateria(mf.getCodigo());
        m.setEstado(mf.isEstado()?"A":"I");
        m.setNombre(mf.getNombre());
        m.setUv(mf.getUv());
        session.save(m);
    }
    
    public List<Materia> buscarMateriaDB(MateriaForm mf) {
        System.out.println("Entre a buscar Materia");
        Criteria crit = session.createCriteria(Materia.class).add(Expression.conjunction().add(Expression.like("nombre", "%" + mf.getNombre() + "%")));
        List<Materia> listMat = new ArrayList<Materia>(crit.list());
        System.out.println("El tamaño de la lista de  becarios es: " + listMat.size());
        return listMat;
    }
    
     public boolean existeCodMateria(MateriaForm mf){
       boolean existe = false;
       Materia m = (Materia) session.get(Materia.class, mf.getCodigo());
       if(m!=null) existe=true;
       return existe;
    }
     
     public void eliminarTodo(MateriaForm mf){
        Query query = session.createQuery("delete from MateriaXCarrera where id.codCarrera = :v_carr and id.idUniv= :v_univ");
        query.setInteger("v_carr", mf.getCodCarrera());
        query.setInteger("v_univ",mf.getIdUniv());
        query.executeUpdate();
     }
     
     public void agregarMateriasCarrera(MateriaForm mf){
         System.out.println("Entre a agregarMateriasCarrera");
        MateriaXCarrera mxc;
        MateriaXCarreraId id;
        if (mf.getSeleccionadas() == null || mf.getSeleccionadas().length == 0)
            return;
        for (int i = 0; i < mf.getSeleccionadas().length; i++){
            id = new MateriaXCarreraId(mf.getSeleccionadas()[i],mf.getCodCarrera(),mf.getIdUniv());
            if((MateriaXCarrera)session.get(MateriaXCarrera.class,id)==null){
                mxc = new MateriaXCarrera();
                mxc.setId(id);
                mxc.setCarrera((Carrera) session.get(Carrera.class,new CarreraId(mf.getCodCarrera(),mf.getIdUniv())));
                mxc.setMateria((Materia) session.get(Materia.class,mf.getCodigo()));
                session.saveOrUpdate(mxc);
            }else{
                System.out.println("No hago nada");
            }
           
        }
    }
     
     public void setMateriaForm(MateriaForm mf) {
        Materia m = (Materia) session.get(Materia.class, mf.getCodigo());
        mf.setCodigo(m.getCodMateria());
        if(m.getEstado().equals("A")){
            mf.setEstado(true);
        }else{
            mf.setEstado(false);
        }
        
        mf.setNombre(m.getNombre());
        mf.setUv(m.getUv());
    }
     
    public void modificarMateria(MateriaForm mf){
        Materia m = (Materia) session.get(Materia.class, mf.getCodigo());
        m.setNombre(mf.getNombre());
        m.setUv(mf.getUv());
        m.setEstado(mf.isEstado()?"A":"F");
        session.update(m);
    }
    
    public List<Carrera> getAllCarreras(MateriaForm mf){
        List<Carrera> allCarreras;
        String hql = "";
        hql = (mf.getIdUniv() != 0) ? "From Carrera where id.idUniv = :univ" : "From Carrera";
        Query query = session.createQuery(hql);
        if (mf.getIdUniv() != 0)
            query.setInteger("univ", mf.getIdUniv());
        allCarreras = new ArrayList<Carrera>(query.list());
        return allCarreras;
    }
    
     public List<Universidad> getUniversidadesActivas(){
        List<Universidad> allUniversidades;
        Query queryResult = session.createQuery("From Universidad where estado='A'");
        allUniversidades = new ArrayList<Universidad>(queryResult.list());
        return allUniversidades;
    }
     
     public List<Materia> getAllMaterias(MateriaForm mf){
        List<Materia> allMaterias;
        String hql = "";
        hql = (mf.getCodCarrera() != 0) ?"Select materia From MateriaXCarrera as mxc where mxc.id.codCarrera = :carrera ":"Select materia From MateriaXCarrera";
        Query query = session.createQuery(hql);
        if(mf.getCodCarrera() !=0)
            query.setInteger("carrera", mf.getCodCarrera());
        allMaterias = new ArrayList<Materia>(query.list());
        return allMaterias;
    }
     
    public void InhabilitarMateriaDB(String codMateria) {
        Materia m = (Materia) session.get(Materia.class,codMateria);
        m.setEstado("I");
        session.update(m);
    }

    public void HabilitarMateriaDB(String codMateria) {
        Materia m = (Materia) session.get(Materia.class,codMateria);
        m.setEstado("A");
        session.update(m);
    }
    
     public List<Materia> obtenerMateriasXCarrera(MateriaForm mf,Becario bec){
        List<Materia> materias;
        String hql2="Select materia From MateriaXCarrera as mxc where mxc.id.codCarrera = :carrera";
        Query query = session.createQuery(hql2);
        int codCarr = mf.getCodCarrera();
        query.setInteger("carrera",codCarr);
        materias = new ArrayList<Materia>(query.list());
        return materias;
    }
    
}
